/*
Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package PictureDescription;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import GuiTools.*;
import Mainframe.Mainframe;
import Settings.*;


/**
 * @author Christoph Schnack
 *
 */
public class PictureManageDialog{
	MDTribuitFrame manageFrame;
	JList vocList;
	Settings SET;
	Mainframe MAINFRAME;
	Language LANG;
	MDManager PMDM;
	PictureStorage PICSTO;
	EditButton editVoc,delVoc,editName;
	JButton editLessonsBtn;
	ListModel vocListModel;
	
	public PictureManageDialog(PictureStorage picsto,Settings set,Mainframe mf,MDManager pmdm){
		SET=set;
		LANG=SET.getLanguage();
		PMDM=pmdm;
		MAINFRAME=mf;
	    manageFrame = new MDTribuitFrame(LANG.getPictureAdministration(),PMDM);
	    PMDM.setCurrentDialog(manageFrame);
	    PICSTO=picsto;
	    boolean hasVocs = PICSTO.hasCollections();
	    EditButton newVoc = new EditButton("+",true);
	    newVoc.addActionListener(new new_vocListener());
	    newVoc.setToolTipText(LANG.getAddVocabulary());
	    delVoc = new EditButton("-",hasVocs);
	    delVoc.addActionListener(new del_vocListener());
	    delVoc.setToolTipText(LANG.getToolTipDelVoc());
	    editVoc = new EditButton("edt",hasVocs);
	    editVoc.addActionListener(new edit_vocListener());
	    editVoc.setToolTipText(LANG.getToolTipEdtVoc());
	    editName = new EditButton("name",hasVocs);
	    editName.addActionListener(new edit_nameListener());
	    editName.setToolTipText(LANG.getToolTipEVN());
	    editLessonsBtn = new JButton(LANG.getEditLessons());
	    editLessonsBtn.setEnabled(hasVocs);
	    editLessonsBtn.addActionListener(new EditLessonsButtonListener());
	    JButton cancelVoc = new JButton(LANG.getClose());
	    cancelVoc.addActionListener(new cancel_vocListener());
	    JPanel button_panel = new JPanel();
	    button_panel.setBackground(GuiTools.BGCOLOR);
	    button_panel.add(newVoc);
	    button_panel.add(delVoc);
	    button_panel.add(editVoc);
	    button_panel.add(editName);
	    button_panel.add(editLessonsBtn);
	    button_panel.add(cancelVoc);
	    vocList = new JList();
	    if(hasVocs){
	    	String[] list_content = PICSTO.getCollectionNames();
	    	vocList.addMouseListener(new ListListener());
	    	vocList.setListData(list_content);
	    }
	    vocList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	    JScrollPane listJsp = new JScrollPane(vocList);
	    listJsp.setPreferredSize(new Dimension(100,80));
	    GridBagConstraints vocgbc = new GridBagConstraints();
	    manageFrame.setLayout(new GridBagLayout());
	    vocgbc.fill = GridBagConstraints.BOTH;
	    manageFrame.add(new Header(LANG.getPictureAdministration()),vocgbc);
	    vocgbc.gridy = 1;
	    manageFrame.add(listJsp,vocgbc);
	    vocgbc.gridy = 2;
	    manageFrame.add(button_panel,vocgbc);
	    manageFrame.showUpAt(MAINFRAME);
	    manageFrame.addWindowListener(new WindowAdapter(){
	    	public void windowClosing(WindowEvent e){
		    	PMDM.setCurrentDialog(null);
		        if(MAINFRAME.getState()==JFrame.ICONIFIED) MAINFRAME.setState(JFrame.NORMAL);
		        MAINFRAME.requestFocus();
	    	}
	    });
	}
	
	
	class new_vocListener implements ActionListener{
	    public void actionPerformed(ActionEvent e){
	    	new AddPicVocDialog();
	    }
	}
	class del_vocListener implements ActionListener{
	    public void actionPerformed(ActionEvent e){
	    	String toDelete = (String)vocList.getSelectedValue();
	    	if(toDelete!=null){
	    		int answer = JOptionPane.showConfirmDialog(manageFrame,new JLabel(LANG.getDeleteReally()+": "+toDelete + " ("+PICSTO.getCollection(toDelete).getPictureUnits().size()+" "+LANG.getPictureUnits()+")"),LANG.getQuestion(), JOptionPane.YES_NO_OPTION);
	    		if( answer==JOptionPane.YES_OPTION){
	    			if(SET.getAutosave()){
	    				int ans = JOptionPane.showConfirmDialog(manageFrame,new JLabel(LANG.getDeleteCollectionIrrecoverably()),LANG.getQuestion(), JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
	    				if( ans!=JOptionPane.YES_OPTION) return;
	    			}
	    			if(SET.isVerbose()) System.out.println("deleting: "+toDelete);
	    			PICSTO.deleteCollection(toDelete);
	    			MAINFRAME.updateMainframe();
	    			updateManageDialog();
	    		}
	    	}
	    	else JOptionPane.showMessageDialog(manageFrame,LANG.getNothingChosen());
	    }
	}
	
	class cancel_vocListener implements ActionListener{
	    public void actionPerformed(ActionEvent e){
	    	manageFrame.dispose();
	    	PMDM.setCurrentDialog(null);
	    }
	}
	
	class ListListener extends MouseAdapter{
		public void mouseClicked(MouseEvent e){
			if(e.getClickCount()>=2){
				String vte = (String)vocList.getSelectedValue();     
		    	if(vte!=null){
		    		new PictureVocabularyManageDialog(PICSTO,SET,MAINFRAME,PMDM,vte);
		    		manageFrame.dispose();
		    	}
		    	else JOptionPane.showMessageDialog(manageFrame,LANG.getNothingChosen());
			}
		}
	}
	class edit_vocListener implements ActionListener{
	    public void actionPerformed(ActionEvent e){
	    	String vte = (String)vocList.getSelectedValue();     
	    	if(vte!=null){
	    		new PictureVocabularyManageDialog(PICSTO,SET,MAINFRAME,PMDM,vte);
	    		manageFrame.dispose();
	    	}
	    	else JOptionPane.showMessageDialog(manageFrame,LANG.getNothingChosen());
	    }
	}
	
	private void enableEditButtons(boolean arg){
		editVoc.setEnabled(arg);
    	delVoc.setEnabled(arg);
    	editName.setEnabled(arg);
    	editLessonsBtn.setEnabled(arg);
	}
	
	class edit_nameListener implements ActionListener{ 
		public void actionPerformed(ActionEvent e){
			String vte = (String)vocList.getSelectedValue();
			if(vte==null) JOptionPane.showMessageDialog(manageFrame,LANG.getNothingChosen());
			else new AddPicVocDialog(vte);
		}
	}
	
	class EditLessonsButtonListener implements ActionListener{ 
		public void actionPerformed(ActionEvent e){
			manageFrame.dispose();
			new PictureLessonsDialog(PICSTO,SET,MAINFRAME,PMDM);
		}
	}
	
	public void updateManageDialog(){
		vocList.removeAll();
		String[] list_content = PICSTO.getCollectionNames();
		vocList.setListData(list_content);
		enableEditButtons(list_content.length>0);
	}
	
	private class AddPicVocDialog extends JDialog{
		String colNameOld="",headerTitle;
		ActionListener okListener;
		JDialog dialog;
		TribuitTextField nameTF;
		public AddPicVocDialog(String cte){
			super(manageFrame);
			dialog=this;
	    	colNameOld = cte;
	    	headerTitle = LANG.getEditVocabulary();
	    	okListener = new avdOkEditListener();
	    	buildAddVocDialog();
	    }
	    
	    /**
	     * create vocabulary constructor
	     */

	    public AddPicVocDialog(){
	    	super(manageFrame);
	    	dialog=this;
	    	headerTitle = LANG.getAddVocabulary();
	    	okListener = new avdOkListener();
	    	buildAddVocDialog();
	    }
	    public void buildAddVocDialog(){
	    	dialog.setTitle(headerTitle);
	    	dialog.setModal(true);
	    	JPanel avdHeaderPanel = new Header(headerTitle);
	    	//TextField
	    	nameTF = new TribuitTextField(SET,colNameOld,20);
	    	nameTF.addActionListener(okListener);
	    	JPanel avdFieldpanel = new JPanel();
	    	avdFieldpanel.setLayout(new GridBagLayout());
	    	GridBagConstraints avdGbc = new GridBagConstraints();
	    	avdGbc.insets = new Insets(20,20,20,20);
	    	avdFieldpanel.add(new LabelPanel(LANG.getName()+":",nameTF),avdGbc);
	    	//buttons
	    	JButton avdOk = new JButton("ok");
	    	avdOk.addActionListener(okListener);
	    	JButton avdCancel = new JButton(LANG.getCancel());
	    	avdCancel.addActionListener(new avdCancelListener());
	    	JPanel avdButtonpanel = new JPanel();
	    	avdButtonpanel.add(avdOk);
	    	avdButtonpanel.add(avdCancel);
	    	avdButtonpanel.setBackground(GuiTools.BGCOLOR);
	    	//frame
	    	dialog.getContentPane().add(BorderLayout.NORTH,avdHeaderPanel);
	    	dialog.getContentPane().add(BorderLayout.CENTER,avdFieldpanel);
	    	dialog.getContentPane().add(BorderLayout.SOUTH,avdButtonpanel);
	    	nameTF.requestFocus();
	    	dialog.pack();
	    	dialog.setLocationRelativeTo(manageFrame);
	    	dialog.setVisible(true);
	    }
	    class avdCancelListener implements ActionListener{
			public void actionPerformed(ActionEvent e){
		    	dialog.dispose();
			}
	    }
	    
	    class avdOkListener implements ActionListener{
			public void actionPerformed(ActionEvent e){
			    String input = nameTF.getText();
			    input=input.trim();
		    	if(!GuiTools.checkInput(input) || input.equals("")){
		    		GuiTools.showEmptyOrInvalidInputWarning(LANG, dialog, LANG.getNewName());
					return;
				}
		    	if(PICSTO.hasCollection(input)){
		    		JOptionPane.showMessageDialog(dialog,LANG.getVocabularyAlreadyExists());
		    		return;
		    	}
				PICSTO.createCollection(input);
				dialog.dispose();
				updateManageDialog();
				MAINFRAME.updateMainframe();
			}
	    }
	    
	    class avdOkEditListener implements ActionListener{
			public void actionPerformed(ActionEvent e){
				String input = nameTF.getText();
			    input=input.trim();
		    	if(!GuiTools.checkInput(input) || input.equals("")){
		    		GuiTools.showEmptyOrInvalidInputWarning(LANG, dialog, LANG.getNewName());
					return;
				}
		    	if(input.equals(colNameOld)){
		    		JOptionPane.showMessageDialog(dialog,LANG.getVocabularyAlreadyExists());
		    		return;
		    	}
				PICSTO.editCollectionName(colNameOld,input);
				dialog.dispose();
				updateManageDialog();
				MAINFRAME.updateMainframe();
			}
	    }
	}
}